<template>
  <div class="login-container">
    <!-- 顶部栏 -->
    <van-nav-bar left-arrow @click-left="$router.push('/')" />
    <h5>您好 , 请登录</h5>
    <!-- 输入框表单 -->
    <van-form class="input-form">
      <!-- 手机输入框 -->
      <van-field
        v-model="mobile"
        left-icon="manager-o"
        type="tel"
        placeholder="请输入手机号"
        name="mobile"
        :rules="[
          { message: '请填写手机号' },
          {
            pattern: /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/,
            message: '手机号格式不对'
          }
        ]"
      />
      <!-- 验证码 -->
      <van-field
        v-model="code"
        left-icon="chat-o"
        placeholder="请输入验证码"
        name="code"
        :rules="[{ validator, message: '验证码格式不对' }]"
      >
        <template #button>
          <van-button :disabled="disabled" size="small" @click="savecode">
            <span v-if="countdown">获取验证码</span>
            <span v-if="!countdown" style="color: #000"
              >{{ count }}s 后重试
            </span>
          </van-button>
        </template>
      </van-field>
      <p class="logintext">
        登录即同意<span>《用户使用协议》</span>和<span>《隐私协议》</span>
      </p>
    </van-form>
    <div style="margin: 16px">
      <van-button
        round
        block
        color="#ee0a24"
        native-type="submit"
        @click="onSubmit"
        loading-text="登录中..."
        >提交</van-button
      >
    </div>
  </div>
</template>

<script>
//导入网络中间层
import { userLogin, logincode } from '@/views/login/user.js'
// //导入token工具
// import { saveToken, getToken } from "@/utils/token.js";

import { mapActions } from 'vuex'
export default {
  data () {
    return {
      mobile: '18888881111',
      code: '',
      countdown: true,
      count: '',
      timer: null,
      disabled: false
    }
  },

  methods: {
    //获取验证码点击事项
    savecode () {
      //验证码倒计时
      const TIME_COUNT = 5
      if (!this.timer) {
        this.count = TIME_COUNT
        this.countdown = false
        this.timer = setInterval(() => {
          if (this.count > 0) {
            this.count--
            this.disabled = true
          } else {
            this.disabled = false
            this.countdown = true
            clearInterval(this.timer)
            this.timer = null
          }
        }, 1000)
      }
      //获取并自动显示验证码
      logincode(this.mobile).then(res => {
        this.$toast.loading({
          message: '加载中...',
          forbidClick: true,
          loadingType: 'spinner'
        })
        this.code = res.data.data
        setTimeout(() => {
          this.$toast(this.code)
        }, 1000)
      })
    },
    // 校验函数返回 true 表示校验通过，false 表示不通过
    validator (val) {
      if (val !== '') {
        return /\d{4}/.test(val)
      }
    },
    //登录点击事项
    // userLogin({ mobile: this.mobile, code: this.code })
    ...mapActions(['login']),
    async onSubmit () {
      try {
        this.$toast.loading({
          message: '登录中...',
          forbidClick: true,
          loadingType: 'spinner'
        })
        await this.login({ mobile: this.mobile, code: this.code })

        //清除toast所有组件的效果
        setTimeout(() => {
          this.$toast.clear()
        }, 800)
        //判断
        //取出跳转登录页面前的路径
        // this.$router.push("/");
        const _redirect = this.$route.query._redirect
        if (_redirect) {
          this.$router.push(_redirect)
        } else {
          this.$router.push('/')
        }
      } catch (error) {
        console.log(error)
        this.$toast.fail('登陆失败')
      }
    }
  }
}
</script>

<style lang="less">
.login-container {
  h5 {
    font-weight: 550;
    margin-top: 100px;
    margin-left: 30px;
    width: 219px;
    height: 50px;
    font-size: 36px;
    font-weight: 600;
    text-align: left;
    color: #222222;
    line-height: 50px;
    letter-spacing: 0px;
  }
  .input-form {
    margin-left: 40px;
    margin-top: 90px;
    .van-field__control {
      margin-left: 20px;
    }
    .van-button--default {
      color: #00b8d4;
      font-size: 28px;
      border: none;
    }
  }
  .logintext {
    font-size: 24px;
    margin-left: 10px;
  }
  .logintext span {
    color: #00b8d4;
  }
  .van-button--normal {
    width: 690px;
    height: 88px;
    background: #e40137;
    border-radius: 44px;
    margin-top: 100px;
  }
}
</style>
